package com.tiny.core.input;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Erick Zanardo
 *
 */
public class MouseManager implements MouseListener {

    private static MouseManager instance;
    private List<MousePos> buttonHistory;

    private MouseManager(){
        buttonHistory = new ArrayList<MousePos>();
    }

    public static MouseManager getInstance() {
        if (instance == null) {
            instance = new MouseManager();
        }
        return instance;
    }

    @Override
    public void mouseClicked(MouseEvent event) {}

    public MousePos getClick(int button) {
        MousePos returnValue = null;
        if (buttonHistory.size() > 0) {
            for (MousePos pos : buttonHistory) {
                if (pos.getButton() == button) {
                    returnValue = pos;
                    break;
                }
            }
            buttonHistory.remove(returnValue);
        }
        return returnValue;
    }

    @Override
    public void mouseEntered(MouseEvent event) {}

    @Override
    public void mouseExited(MouseEvent event) {}

    @Override
    public void mousePressed(MouseEvent event) {
        MousePos pos = new MousePos();
        pos.setButton(event.getButton());
        pos.setX(event.getX());
        pos.setY(event.getY());
        buttonHistory.add(pos);
    }

    @Override
    public void mouseReleased(MouseEvent event) {}

    public void clearCache() {
        buttonHistory.clear();
    }
}
